# coding: utf8

__author__ = 'Tony'


from .strategy import user_login_task, user_keep_task, user_new_task
from .utils import RetPacker
import traceback
import logging


run_log = logging.getLogger('run')
err_log = logging.getLogger('err')


@RetPacker.restful_auto
def login_event(request):
	"""
	用户登录调度事件
	"""
	try:
		run_log.info('trigger login_event')
		user_login_task.task.runner()
		return RetPacker.pack_ret(RetPacker.STATUS_SUCCESS)
	except Exception as ex:
		err_log.exception('login_event')
		return RetPacker.pack_ret(RetPacker.STATUS_FAIL, msg=str(ex))


@RetPacker.restful_auto
def keep_event(request):
	"""
	用户留存管理事件
	"""
	try:
		run_log.info('trigger keep_event')
		count = user_keep_task.task.runner()
		return RetPacker.pack_ret(RetPacker.STATUS_SUCCESS, data={'count': count})
	except Exception as ex:
		err_log.exception('keep_event')
		return RetPacker.pack_ret(RetPacker.STATUS_FAIL, msg=str(ex))


@RetPacker.restful_auto
def new_event(request, count=None):
	"""
	用户新增事件
	:param count: 新增人数，为None则根据预定规则随机生成
	"""
	try:
		if not count:
			run_log.info('trigger create_event, count=%s' % count)
			count = user_new_task.task.runner()
		else:
			run_log.info('trigger new_event')
			user_new_task.task.make_new(abs(int(count)))
		return RetPacker.pack_ret(RetPacker.STATUS_SUCCESS, data={'count': count})
	except Exception as ex:
		err_log.exception('new_event: count=%s' % count)
		return RetPacker.pack_ret(RetPacker.STATUS_FAIL, msg=str(ex))
